import {
  deleteSharedCategory,
  knowledgeClassification,
  knowledgeDisableOpens,
  knowledgeDelete,
  knowledgeDisableOpen,
  knowledgeClassifyName,
  knowledgeMoveClassify,
  knowledgeAddClassify,
  knowledupOrDownCategory,
  knowledgeSeekList,
} from '@/services/knowledgemanagement';

export default {
  namespace: 'classifyTrees',

  state: {
    loading:true
  },

  // ----- leftTree------
  effects: {
    *fetch({ payload, callback }, { call, put }) {
      // 树列表
      const response = yield call(knowledgeClassification, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *delete({ payload, callback }, { call, put }) {
      // 删除节点
      const response = yield call(knowledgeDelete, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchc({ payload, callback }, { call, put }) {
      // 启用分类
      const response = yield call(knowledgeDisableOpen, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchcs({ payload, callback }, { call, put }) {
      // 停用
      const response = yield call(knowledgeDisableOpens, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *ClassifyName({ payload, callback }, { call, put }) {
      // 修改分类名称
      const response = yield call(knowledgeClassifyName, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *moveClassify({ payload, callback }, { call, put }) {
      // 移动分类
      const response = yield call(knowledgeMoveClassify, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addClassify({ payload, callback }, { call, put }) {
      // 添加分类

      const response = yield call(knowledgeAddClassify, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *upOrDownCategory({ payload, callback }, { call, put }) {
      // 上移下移分类
      const response = yield call(knowledupOrDownCategory, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *fetchListBySearch({ payload, callback }, { call, put }) {
      // 上移下移分类
      const response = yield call(knowledgeSeekList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *deleteSharedCategory({ payload, callback }, { call, put }) {
      // 获取共享的分类树
      const response = yield call(deleteSharedCategory, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  // ----- leftTree结束------

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {};
    },
  },
};
